import { Args, Mutation, Resolver } from '@nestjs/graphql'
// import { Query } from '@nestjs/common';
// import { OSSType } from './dto/oss.type';
import { AuthService } from './auth.service';
import { UserService } from '../user/user.service';
import dayjs from 'dayjs';
import { Result } from '@/common/dto/result.type';
import { ACCOUNT_NOT_EXIST, CODE_EXPIRE, CODE_NOT_EXIST, LOGIN_ERROR, SUCCESS } from '@/common/constants/code';
import { JwtService } from '@nestjs/jwt';
@Resolver()
export class AuthResolver {
    constructor(
        private readonly authService: AuthService,
        private readonly userService: UserService,
        private readonly jwtService: JwtService,
    ) { }
    // @Query(() => String, { description: '获取OSS相关信息' })
    // async getOSSInfo(): Promise<string> {
    //     return await this.authService.sendCodeMsg('123456')
    // }
    @Mutation(() => Result, { description: '发送短信验证码' })
    async sendCodeMsg(@Args('tel') tel:string): Promise<Result> {
        return await this.authService.sendCodeMsg(tel)
    }
    @Mutation(() => Result, { description: '发送短信验证码' })
    async login(@Args('tel') tel:string,@Args('code') code:string): Promise<Result> {
        const user = await this.userService.findByTel(tel)
        // console.log(user)
        if(!user){
            return {
                code:ACCOUNT_NOT_EXIST,
                message:'账号不存在',
            }
        }
        if(!user.codeCreateTimeAt || !user.code){
            return {
                code:CODE_NOT_EXIST,
                message:'code 不存在',
            }
        }
        // console.log('if(user.codeCreateTimeAt && user.code){',user)
        // 检查一小时超时
        if(dayjs().diff(dayjs(user.codeCreateTimeAt))>60*60*1000){
            return {
                code:CODE_EXPIRE,
                message:'code 过期',
            }
        }
        if(user.code == code){
            const token = this.jwtService.sign({
                id:user.id
            })
            return {
                code:SUCCESS,
                message:'登录成功',
                data:token,
            }
        }
        return {
            code:LOGIN_ERROR,
            message:'登录失败，手机号或验证码不匹配',
        }
        // return await this.authService.login(tel)
    }
}